<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="活动编号" prop="no">
        <el-input
          v-model="queryParams.no"
          placeholder="请输入活动编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动主题" prop="subject">
        <el-input
          v-model="queryParams.subject"
          placeholder="请输入活动主题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择活动类型" clearable size="small">
          <el-option
          v-for="dict in activityTypeOptions"
          :key="dict.dictValue"
          :label="dict.dictLabel"
          :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="活动状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择活动状态" clearable size="small">
          <el-option
          v-for="dict in activityStatusOptions"
          :key="dict.dictValue"
          :label="dict.dictLabel"
          :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户奖励产品名称" prop="customerRewardProductName">
        <el-input
          v-model="queryParams.customerRewardProductName"
          placeholder="请输入用户奖励产品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商户奖励类型" prop="merchantRewardType">
        <el-select v-model="queryParams.merchantRewardType" placeholder="请选择商户奖励类型" clearable size="small">
          <el-option
          v-for="dict in activityRewardTypeOptions"
          :key="dict.dictValue"
          :label="dict.dictLabel"
          :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="活动归属商户名称" prop="belongMerchantName">
        <el-input
          v-model="queryParams.belongMerchantName"
          placeholder="请输入活动归属商户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <router-link :to="{name:'activityInfoAdd'}">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            v-hasPermi="['activity:info:add']"
          >新增</el-button>
        </el-col>
      </router-link>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['activity:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['activity:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['activity:info:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange" :border="true">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="活动编号" align="center" prop="no" />
      <el-table-column label="活动主题" align="center" prop="subject" />
      <el-table-column label="活动类型" align="center" prop="type" :formatter="actTypeFmt" />
      <el-table-column label="活动开始时间" align="center" prop="startDate" />
      <el-table-column label="活动结束时间" align="center" prop="endDate" />
      <el-table-column label="活动外链" align="center" prop="linkUrl" />
      <el-table-column label="活动归属商户名称" align="center" prop="belongMerchantName" />
      <el-table-column label="活动状态" align="center" prop="status" :formatter="actStatusFmt" />
      <el-table-column label="用户奖励产品名称" align="center" prop="customerRewardProductName" />
      <el-table-column label="商户奖励类型" align="center" prop="merchantRewardType" :formatter="actRewardTypeFmt" />
      <el-table-column label="商户奖励数值" align="center" prop="merchantRewardNumber" />
      <el-table-column label="审核状态" align="center" prop="examineResult" :formatter="examineResultFmt" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <router-link :to="{name:'activityInfoView',params:{no:scope.row.no}}">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              v-hasPermi="['activity:info:query']"
            >详情</el-button>
          </router-link>
          <router-link :to="{name:'activityInfoView',params:{no:scope.row.no}}">
            <el-button
              v-if="scope.row.examineResult == 0"
              size="mini"
              type="text"
              icon="el-icon-view"
              v-hasPermi="['activity:info:audit']"
            >审核</el-button>
          </router-link>
          <router-link :to="{name:'activityInfoAdd',params:{no:scope.row.no}}">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              v-hasPermi="['activity:info:edit']"
            >修改</el-button>
          </router-link>
          <el-button
            v-if="scope.row.examineResult == 1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handlePush(scope.row)"
            v-hasPermi="['activity:info:push']"
          >推送</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['activity:info:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改活动信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="活动编号" prop="no">
          <el-input v-model="form.no" placeholder="请输入活动编号" />
        </el-form-item>
        <el-form-item label="活动主题" prop="subject">
          <el-input v-model="form.subject" placeholder="请输入活动主题" />
        </el-form-item>
        <el-form-item label="活动内容" prop="content">
          <el-input v-model="form.content" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="活动类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择活动类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="活动开始时间" prop="startDate">
          <el-input v-model="form.startDate" placeholder="请输入活动开始时间" />
        </el-form-item>
        <el-form-item label="活动结束时间" prop="endDate">
          <el-input v-model="form.endDate" placeholder="请输入活动结束时间" />
        </el-form-item>
        <el-form-item label="活动主图">
          <uploadImage v-model="form.showMainImg"/>
        </el-form-item>
        <el-form-item label="活动图片1">
          <uploadImage v-model="form.showImg1"/>
        </el-form-item>
        <el-form-item label="活动图片2">
          <uploadImage v-model="form.showImg2"/>
        </el-form-item>
        <el-form-item label="活动图片3">
          <uploadImage v-model="form.showImg3"/>
        </el-form-item>
        <el-form-item label="活动外链" prop="linkUrl">
          <el-input v-model="form.linkUrl" placeholder="请输入活动外链" />
        </el-form-item>
        <el-form-item label="银行编号" prop="bankNo">
          <el-select v-model="form.bankNo" placeholder="请选择银行编号">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="银行名称" prop="bankName">
          <el-input v-model="form.bankName" placeholder="请输入银行名称" />
        </el-form-item>
        <el-form-item label="活动状态">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="用户奖励产品编号" prop="customerRewardProductNo">
          <el-input v-model="form.customerRewardProductNo" placeholder="请输入用户奖励产品编号" />
        </el-form-item>
        <el-form-item label="用户奖励产品名称" prop="customerRewardProductName">
          <el-input v-model="form.customerRewardProductName" placeholder="请输入用户奖励产品名称" />
        </el-form-item>
        <el-form-item label="商户奖励类型" prop="merchantRewardType">
          <el-select v-model="form.merchantRewardType" placeholder="请选择商户奖励类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="商户奖励比例" prop="merchantRewardNumber">
          <el-input v-model="form.merchantRewardNumber" placeholder="请输入商户奖励比例" />
        </el-form-item>
        <el-form-item label="活动归属商户" prop="belongMerchantNo">
          <el-input v-model="form.belongMerchantNo" placeholder="请输入活动归属商户" />
        </el-form-item>
        <el-form-item label="活动归属商户名称" prop="belongMerchantName">
          <el-input v-model="form.belongMerchantName" placeholder="请输入活动归属商户名称" />
        </el-form-item>
        <el-form-item label="审核编号" prop="examineCode">
          <el-input v-model="form.examineCode" placeholder="请输入审核编号" />
        </el-form-item>
        <el-form-item label="推送方式编号" prop="activityPushNo">
          <el-input v-model="form.activityPushNo" placeholder="请输入推送方式编号" />
        </el-form-item>
        <el-form-item label="创建人" prop="buildOperator">
          <el-input v-model="form.buildOperator" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="创建日期" prop="buildDate">
          <el-input v-model="form.buildDate" placeholder="请输入创建日期" />
        </el-form-item>
        <el-form-item label="创建时间" prop="buildTime">
          <el-input v-model="form.buildTime" placeholder="请输入创建时间" />
        </el-form-item>
        <el-form-item label="修改人" prop="modifiedOperator">
          <el-input v-model="form.modifiedOperator" placeholder="请输入修改人" />
        </el-form-item>
        <el-form-item label="修改日期" prop="modifiedDate">
          <el-input v-model="form.modifiedDate" placeholder="请输入修改日期" />
        </el-form-item>
        <el-form-item label="修改时间" prop="modifiedTime">
          <el-input v-model="form.modifiedTime" placeholder="请输入修改时间" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo, exportInfo, getInfoVo, addInfoVo, updateInfoVo, logicDelInfo, getInfoVoByNo, listInfoVo, infoPush } from "@/api/activity/info";
import UploadImage from '@/components/UploadImage';

export default {
  name: "Info",
  components: {
    UploadImage,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中数组
      nos: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 活动信息表格数据
      infoList: [],
      // 活动类型字典
      activityTypeOptions: [],
      // 活动奖励类型字典
      activityRewardTypeOptions: [],
      // 活动奖励类型字典
      activityStatusOptions: [],
      // 审核状态字典
      examineResultOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        no: null,
        subject: null,
        content: null,
        type: null,
        startDate: null,
        endDate: null,
        showMainImg: null,
        showImg1: null,
        showImg2: null,
        showImg3: null,
        linkUrl: null,
        bankNo: null,
        bankName: null,
        status: null,
        customerRewardProductNo: null,
        customerRewardProductName: null,
        merchantRewardType: null,
        merchantRewardNumber: null,
        belongMerchantNo: null,
        belongMerchantName: null,
        examineCode: null,
        activityPushNo: null,
        buildOperator: null,
        buildDate: null,
        buildTime: null,
        modifiedOperator: null,
        modifiedDate: null,
        modifiedTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  //每次进入页面时触发
  activated(){
    var no = this.$route.query.no;
    if(no){
      this.queryParams.belongMerchantNo = no;
    }
    this.getList();
  },
  created() {
    var no = this.$route.query.no;
    if(no){
      this.queryParams.belongMerchantNo = no;
    }
    this.getList();
    this.getDicts("act_activity_type").then(response => {
      this.activityTypeOptions = response.data;
    });
    this.getDicts("act_activity_reward_type").then(response => {
      this.activityRewardTypeOptions = response.data;
    });
    this.getDicts("act_activity_status").then(response => {
      this.activityStatusOptions = response.data;
    });
    this.getDicts("msg_message_template_examineResult_type").then(response => {
      this.examineResultOptions = response.data;
    });
  },
  methods: {
    /** 查询活动信息列表 */
    getList() {
      this.loading = true;
      listInfoVo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        no: null,
        subject: null,
        content: null,
        type: null,
        startDate: null,
        endDate: null,
        showMainImg: null,
        showImg1: null,
        showImg2: null,
        showImg3: null,
        linkUrl: null,
        bankNo: null,
        bankName: null,
        status: "0",
        customerRewardProductNo: null,
        customerRewardProductName: null,
        merchantRewardType: null,
        merchantRewardNumber: null,
        belongMerchantNo: null,
        belongMerchantName: null,
        examineCode: null,
        activityPushNo: null,
        buildOperator: null,
        buildDate: null,
        buildTime: null,
        modifiedOperator: null,
        modifiedDate: null,
        modifiedTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.nos = selection.map(item => item.no)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加活动信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改活动信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 推送按钮操作 */
    handlePush(row) {
      infoPush(row.no).then(response => {
        this.msgSuccess("推送成功");
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const nos = row.no || this.nos;
      this.$confirm('是否确认删除活动信息编号为"' + nos + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return logicDelInfo(nos);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有活动信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportInfo(queryParams);
        }).catch(() => {});
    },
    /** 活动类型格式化 */
    actTypeFmt(row, column) {
      if(!row.type){
        return "";
      }
      var val = row.type.trim();
      this.activityTypeOptions.forEach(element => {
        if(val == element.dictValue){
          val = element.dictLabel;
        }
      });
      return val;
    },
    /** 活动状态格式化 */
    actStatusFmt(row, column) {
      if(!row.status){
        return "";
      }
      var val = row.status.trim();
      this.activityStatusOptions.forEach(element => {
        if(val == element.dictValue){
          val = element.dictLabel;
        }
      });
      return val;
    },
    /** 商户奖励类型格式化 */
    actRewardTypeFmt(row, column) {
      if(!row.merchantRewardType){
        return "";
      }
      var val = row.merchantRewardType.trim();
      this.activityRewardTypeOptions.forEach(element => {
        if(val == element.dictValue){
          val = element.dictLabel;
        }
      });
      return val;
    },
    /** 审核状态格式化 */
    examineResultFmt(row, column) {
      if(!row.examineResult){
        return "";
      }
      var val = row.examineResult.trim();
      this.examineResultOptions.forEach(element => {
        if(val == element.dictValue){
          val = element.dictLabel;
        }
      });
      return val;
    }
  }
};
</script>
